Incremental query evaluation

ABSTRACT

A system and method for implementing incremental evaluation that is advantageous over nonincremental evaluation. The system and method incrementally maintain each individual aggregate value, for non-nested queries, using a constant amount of work.

BACKGROUND

The system and method of the present embodiment relate generally to processing queries using incremental processing techniques.

Modem relational databases focus almost exclusively on providing flexible and extensible querying capabilities in dynamic environments, supporting both schema and data evolution. As a consequence, today's database management systems are centered around highly flexible interactive query processors, with their plan interpreters and other runtime components including schedulers and optimizers. However, a large fraction of the world's query workloads are fixed and embedded into database application programs. In these applications, queries are ossified during the development cycle, with developers converging to a choice of schema, query structure and parameters. Once hardened, queries are deployed into production environments, and reused numerous times, executing non-interactively. Other stream processing engines and relational databases provide a development environment for authoring stream processing applications. Data stream processing addresses the problem of processing standing queries over a limited form of database, namely sliding windows on sequential data. A window restricts the set of data to be processed to a very specific subset, typically a recent period of time. Incremental view maintenance on the other hand considers queries on a general database, that can be modified in any manner, and does not need to consist of only recent data. However the limitation of view maintenance lies in the class of queries supported, especially in terms of nested aggregates, and subqueries, and has been shown to have problems scaling to handle large volumes of changing data. Thus neither of these solutions is sufficient for financial, monitoring, and database applications, among others.

Most databases evolve incrementally, through changes that are small compared to the overall database size. For queries that are asked repeatedly, memoization of the query result can substantially reduce the amount of work needed to re-evaluate the query after moderate changes to the database. Using stored query results or auxiliary data structures to this effect is known as incremental view maintenance. A key notion in incremental view maintenance is that of a delta query. A change (“update”, which captures both insertions and deletions) to database D is denoted by u. The updated database is denoted by D+u, where + is a generalization of the union operation of relational algebra. A delta query Δ_(u)Q depends on both D and u and expresses the change to the result of query Q as D is updated to D+u:

Q(D+u)=Q(D)+Δ_(u) Q(D).

Evaluating Δ_(u)Q(D)and using the result to update a materialized representation of Q(D) can be faster than recomputing Q(D+u) from scratch, because ΔQ is a simpler query than Q. Queries for which this is not true, e.g., queries with aggregates nested into conditions, may not be included in incremental view maintenance studies. It may not be clear that incremental view maintenance is more efficient than nonincremental query evaluation. If a query language L with multiset semantics is closed under joins (such as SQL), the image of L under taking deltas is the full language L: Given an arbitrary query Q₀ ∈ L, there is another query Q ∈ L and a single-tuple insertion u such that Δ_(u)Q=Q₀. If u is an arbitrary single-tuple insertion into a relation R that does not occur in Q₀, then Q is Q₀×π_(Ø)R, which suggests that incremental view maintenance is not advantageous over nonincremental query evaluation. A system and method are needed that implement incremental evaluation that is advantageous over nonincremental evaluation. A system and method are further needed for incrementally maintaining each individual aggregate value, for non-nested queries, using a constant amount of work per data item or data value incrementally maintained. A system and method are still further needed to develop automated trading algorithms having reduced development time and better throughput and lower latency for queries over level II (i.e. orderbook) data. A system and method are even still further needed for a query processing framework. This framework could be used, for example, but not limited to, for monitoring applications that could enable uniformly using the core of a high-level declarative language such as SQL across the multiple heterogeneous computing platforms present in such applications, by transforming to low-level code and customizing such code to execute on embedded devices such sensor motes, and cell phones.

SUMMARY

The needs set forth above as well as further and other needs and advantages are addressed by the embodiments set forth below. The present embodiment solves the problem of processing standing queries over a rapidly, and arbitrarily changing database, using incremental processing techniques.

The present embodiment provides incremental view maintenance based on an algebraic perspective. In the present embodiment, a ring of databases can be used as the foundation of a query calculus that allows expression of aggregate queries. The query calculus can inherit properties of the ring, such as having a normal form of polynomials and being closed under computing inverses and delta queries. The k-th delta of a polynomial query of degree k without nesting becomes, in the present embodiment, a function of the update, not of the database. Using the present embodiment, expensive query operators such as joins can be eliminated from programs that perform incremental view maintenance. The main result is that, for non-nested queries, each individual aggregate value can be incrementally maintained using a constant amount of work. This is not possible for nonincremental evaluation.

The present embodiment is related to both data stream processing, and incremental view maintenance. As described, the delta of a query is a query itself. Subsequently, a k^(th) delta of a query, also called a query of the k^(th) level, and an input specification is a specification of level zero.

The system and method of the present embodiment can recursively apply compilation to transform delta forms such as, for example, queries, to simpler and simpler queries, by considering combinations of base relation deltas. Recursive compilation in the present embodiment completes with queries that can be represented as simple procedural code statements. The system and method of the present embodiment can maintain each delta form encountered as a map datastructure, essentially a group-by aggregate index derived from applying aggregate distributivity properties together with join-graph decompositions. The procedural code generated by recursively compiling the delta forms maps can incrementally maintain the maps. The system and method can be enabled by an exemplary map algebra that can be used to represent and reason about queries and map datastructures, and can be used to perform recursive compilation through a set of transformation rules defined in the map algebra.

The present embodiment can include a framework by which SQL aggregate queries can be compiled into C++ code for continuous, incremental view maintenance. The SQL query compilation framework of the present embodiment can generate native code that can incrementally and continuously maintain aggregate views, for example, but not limited to, in main memory, using aggressive delta processing techniques. The system and method of the present embodiment can recursively compile queries into incremental view maintenance code; that is, instead of expressing and evaluating increments to queries as queries, the system and method of the present embodiment recursively compute increments to these increments, and so on, which can eliminate all queries. The system and method of the present embodiment can include, but are not limited to including, an aggressive compilation mechanism for transforming SQL queries to low-level code. Aggressive compilation refers to recursive compilation of queries into code. View maintenance algorithms typically consider a type of modification (insert, update, or delete) to a view, and derive a query to process this modification. However, this is only a single level of incremental computation, and the job of view maintenance overall still relies on queries. On the contrary, the system and method of the present embodiment recursively apply compilation on these resulting queries, until all queries are eliminated, leaving low-level code. Characteristics of aggressive compilation can include, but are not limited to including, aggressive inlining and rule-based transformation of a SQL query plan to produce event- handling functions capable of processing queries and producing results. The system and method of the present embodiment produce one event handling function for each type of modification event (insert, update, or delete) on any input relation to the query. Aggressive compilation can replace query plans (i.e. graphs) with event handling functions. Query graphs typically include dataflow between operators, which in turn hold queues and data structures as state to support their computation. The system and method of the present embodiment can transform and inline the operators, completely eliminating queues, and can determine which data structures to maintain to support the query, in its entirety, and in particular for each type of modification event, rather than at the individual operator level. In the system and method of the present embodiment, data structures can be reused across different modification events.

For a better understanding of the present embodiments, together with other and further embodiments thereof, reference is made to the accompanying drawings and detailed description.

DESCRIPTION OF THE SEVERAL VIEWS OF THE DRAWING

FIG. 1 is a table of results from recursive memoization of deltas for f(x)=3x²;

FIG. 2 is a table of a trace of map q that changes during a sequence of insertions and deletions;

FIG. 3 is a schematic block diagram of the system of the present embodiment; and

FIG. 4 is a flowchart of the method of the present embodiment.

DETAILED DESCRIPTION

The present embodiments are now described more fully hereinafter with reference to the accompanying drawings. The following configuration description is presented for illustrative purposes only. Any computer configuration and architecture satisfying the speed and interface requirements herein described may be suitable for implementing the system and method of the present embodiments.

The present embodiment can be described with reference to function f: A→B such that (A, +) is an additive semigroup (i.e, set A is closed under a binary operation +) and (B, +) is an additive group (i.e., a semigroup with an additive identity 0^(B) and an additive inverse). For an u ∈ A, function Δ_(u)f:A→B is defined as

Δ_(u) f: x

f(x+u)−f(x).

Δ_(u) f (x) is the amount of change that happens to the value of f (x) as the value of x is changed to x+u (“updated by u”).

Function Δ^(j)f of j -th deltas for j≧0 is defined as

A×U ^(j) →B<x, u ₁ , . . . , u _(j)>

(Δ_(u) _(j) (Δ_(u) _(j−1) . . . (Δ_(u) _(t) f) . . . ))(x).

where Δ⁰f is f. x ∈ A is considered current. x can be updated with increments from a set of possible updates U ⊂ A and to obtain f(x) without having to recompute it “from scratch” after each change to x. f has the property that there is a k ∈ N such that Δ^(k)f is 0^(B) on all inputs. If 0≦j≦k and {right arrow over (u)} ∈ U^(j), the values Δ^(j)f (x, {right arrow over (u)}) for the current x are known. If additions and subtractions can be performed cheaply, but it is expensive to compute Δ^(j)f (x_(new), {right arrow over (u)}) from x_(new) (j≧0). On update event x_(new):=x_(cur)+u, compute

Δ^(j) f(x _(new) ,{right arrow over (u)}):=Δ^(j) f(x _(curr) , {right arrow over (u)})+Δ^(j+1) f(x _(curr) ,{right arrow over (u)},u),

for all 0≦j<k and {right arrow over (u)} ∈ U^(j). This requires only additions of memorized Δ^(j) f values. If the sums are computed in order of increasing j, double-buffering may not be necessary but the values can be updated in place, i.e., Δ^(j)f(x,{right arrow over (u)})+=Δ^(j+1)f(x,{right arrow over (u)},u). None of these values have to be recomputed from their (function) definitions. For example, if f(x)=3x²,

$\begin{matrix} {{\Delta \; {f\left( {x,u_{1}} \right)}} = {{f\left( {x + u_{1}} \right)} - {f(x)}}} \\ {= {{3\left( {x + u_{1}} \right)^{2}} - {3x^{2}}}} \\ {= {{6u_{1}x} + {3u_{1}^{2}}}} \end{matrix}.\begin{matrix} {{\Delta^{2}{f\left( {x,u_{1},u_{2}} \right)}} = {\left( {\Delta \; {f\left( {{x + u_{2}},u_{1}} \right)}} \right) - {\Delta \; {f\left( {x,u_{1}} \right)}}}} \\ {= {{6{u_{1}\left( {x + u_{2}} \right)}} + {3u_{1}^{2}} - \left( {{6u_{1}x} + {3u_{1}^{2}}} \right)}} \\ {= {6u_{1}u_{2}}} \end{matrix}.\begin{matrix} {{\Delta^{3}{f\left( {x,u_{1},u_{2},u_{3}} \right)}} = {\left( {\Delta^{2}{f\left( {{x + u_{3}},u_{1},u_{2}} \right)}} \right) - {\Delta^{2}{f\left( {x,u_{1},u_{2}} \right)}}}} \\ {= {{6u_{1}u_{2}} - {6u_{1}u_{2}}}} \\ {= 0.} \end{matrix}$

The set of possible updates is U=±1, seven function values are memorized, and values are computed for them from the definitions of f, Δf, and Δ²f. |U|⁰+|U|¹+|U|²=7 values are computed from the definitions only once, for the initial x value, and additions of the current seven values are performed. FIG. 1 shows values for x=0, . . . , 4. At any point in time (for any current x value), one row of this table, with seven values, can be held in memory, and x is incremented or decremented by one, which corresponds to changing the memorized row to its predecessor or successor in the table of FIG. 1. To perform this replacement, one other memorized value is added to each memorized value. For example, if x=3 and x is incremented by 1. Then f (x)+=21=48, Δ^(t)f (x,+1)+=6=27, Δ¹f(x, −1)+=−6=−15, and Δ²f (x,•,•)+=0. This memoization technique can be converted to an incremental view maintenance algorithm as follows. The functions f: A→B can be queries, and A can be a domain of databases. For this analogy to hold, the query language L must have two properties: the query language L has to be closed under taking deltas, that is, for each query of L, a delta query has to be expressible in L; L has to support the addition of a query and its delta; and for each query, there must be a k ∈ N such that the k -th delta query returns constantly the empty database.

Given a query f, this technique memorizes a hierarchy of materialized views representing the functions

<u₁, . . . , u_(j)>

Δ^(j)f(x,u₁, . . . , u_(j))

for 0≦j≦k. x is the current database. The (active) domains of these functions are finite, and their graphs can be captured by group-by queries, grouping the results by the tuples <u₁, . . . , u_(j)>. For example, the following SQL query over unary relation R with schema A and multiset semantics can be stated as

Q(R)=select count(*) from R r1, R r2 where r1.A=r2.A

The delta queries for single-tuple insertions (denoted +R(a)) and deletions (denoted −R(a)) are

ΔQ(R,±R(a))=1±2*

(select count(*) from R where R.A=a)

${\Delta^{2}{Q\left( {R,{\pm_{1}{R\left( a_{1} \right)}},{\pm_{2}{R\left( a_{2} \right)}}} \right)}} = \left\{ \begin{matrix} {\pm_{1}{\pm_{2}2}} & \ldots & {a_{1} = a_{2}} \\ 0 & \ldots & {otherwise} \end{matrix} \right.$

and Δ′Q(R,•,•,•)=0.

The above definitions give Q(Ø)=0 and ΔQ(Ø, ±R(·))=1.

ΔQ(R, •) Update R Q(R) +R(c) −R(c) +R(d) −R(d) Ø 0 1 1 1 1 +R(c) {c} 1 3 −1 1 1 +R(c) {c, c} 4 5 −3 1 1 +R(d) {c, c, d} 5 5 −3 3 −1 +R(c) {c, c, c, d} 10 7 −5 1 −1 −R(d) {c, c, c} 9 7 −5 1 1 +R(c) {c, c, c, c} 16 9 −7 1 1 −R(c) {c, c, c} 9 7 −5 1 1

In the above table, Δ²Q is omitted for space reasons. It is constant (does not depend on the database), and

Δ² Q(R,+R(a),+R(a))=Δ² Q(R,−R(a),−R(a))=2,

Δ² Q(R, +R(a),−R(a))=Δ² Q(R,−R(a),+R(a))=−2,

and Δ² Q(R,± ₁ R(a ₁), ±₂ R(a ₂))=0 for a ₁ ≠a ₂.

Queries are evaluated and the database is accessed to initialize the memorized values. After that, the update rule is used

Δ^(j) f(x _(new) , {right arrow over (u)}):=Δ^(j) f(x _(curr) ,{right arrow over (u)})+Δ^(j+1) f(x _(curr) ,{right arrow over (u)},u),

to apply an update u. In this incremental view maintenance technique delta processing is performed recursively along with materializing view Q and using ΔQ to update it. ΔQ is materialized, incrementally maintained using Δ²Q , the result is materialized, and so on.

The query language that is required for the present embodiment encompasses a large class of SQL (aggregate) queries. The algebraic structure of a ring of databases of the present invention includes an addition operation that generalizes relational union, and a multiplication operation that generalizes the natural join. The elements of the ring are generalizations of multi-set relational databases in which tuples can have negative multiplicities (to model deletions). There are further generalizations shown to be necessary in the present embodiment, in addition to a full additive inverse (necessary for delta processing) and distributivity (to yield polynomials). The ring is the basis of the definition of an expressive aggregate query calculus, AGCA, which inherits these key properties. For an AGCA query Q without nested aggregates, Act exists and is structurally strictly simpler than Q. The k-th delta of an AGCA query of degree k without nested aggregates has degree 0; a query of degree 0 only depends on the update but not on the database. Thus, the recursive delta processing technique of the present embodiment is applicable to AGCA. Each query can be incrementally maintained by a hierarchy of materialized views, requiring only basic summations of values in adjacent layers of the view hierarchy to keep all views up to date. Only a single arithmetic operation is required per single-tuple update and per value maintained in this hierarchy of views. For the purpose of aggregate query processing, these values are numerical. In practice, such values can be represented using fixed-size memory words.

The present embodiment can maintain finite active domains of the memorized functions, and can extend these when updates require it. More fundamentally, a j-th delta is a function of a j-tuple of update tuples, which means that its domain and the, for example, tabular representation of the memorized function may become large. The present embodiment can employ query factorization and simplification of queries, which can allow the creation of small representations of the materialized views. The present embodiment includes a method for compiling queries to a simple low-level language which can be, but is not limited to being, related to the programming language C. The present embodiment can eliminate all joins and evaluate queries without resorting to classical query operators. The compiler of the present embodiment produces, for example, source code or object code, that can be edited or simply linked in with existing client application code as, for example, an embeddable query processor.

The notations f: x

v, f(x):=v, and f:={x

v

x ∈ dom(f)} are used interchangeably to define functions, with a preference for the latter when the domain dom(f) might otherwise remain unclear. f|_(D) denotes the restriction of the domain of f to D, i.e. f|_(D):={(x

v)∈ f

x ∈ D). A function f: A→B is called surjective if for all b ∈ B, there is an a ∈ A such that f(a)=b.

A semigroup is a pair (A, °) of a base set A and a binary total function ∘:A×A→A (“the operation”) such that a is associative, that is, for all a, b, c ∈ A, (a◯b)∘c=a∘(b∘c). A semigroup is called commutative if a∘b=b∘a for all a, b ∈ A. A monoid (A,∘,e) is a semigroup that has neutral element e ∈ A, that is, a∘e=e∘a=a for all a ∈ A. A monoid is called a group if for each a ∈ A there is an inverse element a⁻¹ ∈ A such that a∘a⁻¹=a⁻¹∘a=e.

A ring over base set A is a tuple (A,+,*,0) with two operations + and * (called addition and multiplication, respectively) such that (A,+, 0) is a commutative group, (A,*) is a semigroup, and + and * are distributive, that is, a*(b+c)=a*b+a*c and (a+b)*c=a*c+b*c for all a, b, c ∈ A. A ring with identity (A,+,*, 0,1) is a ring in which (A,*,1) is a monoid. A ring is called commutative if * is commutative. For example, the integers Z and the rational numbers Q form commutative rings with identity (Z,+,*, 0,1) and (Q,+,*,0,1). The natural numbers N do not form a ring because there is no additive inverse; for example, there is no natural number x such that 1+x=0.

Neutral elements 0 and 1 are named by analogy and are not necessarily numbers. For a group with an operation +, −a denotes a⁻¹ and the shortcut a−b denotes a +(−b). When the operations + and * are clear from the context, the name of the base set is used herein to denote the ring (e.g., Z for (Z,+,*,0,1)). In a monoid, there is a unique identity element and in a group (A,∘,e), there is a unique inverse element a⁻for each element a ∈ A. Thus, in particular, a ring is uniquely determined by its base set and its operations + and *.

A is a commutative ring such as the integers Z, the rational numbers Q, or the real numbers R. A structure (A_(Rel),+,*,0,1) (or just A_(Rel), for short) of generalized multiset relations denotes collections of tuples with multiplicities from A and possibly differing schemas. The operations + and * are generalizations of multiset union and natural join, respectively, to total functions (i.e., applicable to any pair of elements of A_(Rel)). The schema polymorphism of tuples in generalized multiset relations serves the purpose of accommodating such total operator definitions.

A (typed) tuple {right arrow over (t)} is a partial function from a vocabulary of column names dom({right arrow over (t)}) to data values (that is, {right arrow over (t)} is not just a tuple of values but has an associated schema of its own). In the present description, all tuples are typed, and Tup denotes the set of all typed tuples. {{right arrow over (t)}} denotes construct a singleton relation with schema sch({{right arrow over (t)}})=dom({right arrow over (t)}) from {right arrow over (t)} and the classical natural join operator

a is used on such single tons. The set of all singletons is denoted by Sng (i.e., Sng:={{{right arrow over (t)}}|{right arrow over (t)} ∈ Tup}). For a commutative ring A, a generalized multiset relation (gmr) is a function R: Tup→A such that R({right arrow over (t)})≠0 for at most a finite number of tuples {right arrow over (t)}. The set of all such functions is denoted by A_(Rel) . Such a function indicates the multiplicity with which each tuple of Tup occurs in the gmr. Tuples can have negative multiplicities. The operations + and * of A_(Rel) are defined as follows.

${{For}\mspace{14mu} R},{S \in A_{Rel}},{R + {S:\left. \overset{->}{x}\mapsto\left( {{R\left( \overset{->}{x} \right)} + {S\left( \overset{->}{x} \right)}} \right) \right.}}$ $R*{S:\left. \overset{->}{x}\mapsto{\sum\limits_{{\{\overset{\_}{x}\}} = {{\{\overset{\_}{a}\}}{\{\overset{\_}{b}\}}}}{{R\left( \overset{->}{a} \right)}*{S\left( \overset{->}{b} \right)}}} \right.}$

It follows:

$\left( {- R} \right):\left. \overset{->}{x}\mapsto\left( {- {R\left( \overset{->}{x} \right)}} \right) \right.$ $1:\left. \overset{->}{x}\mapsto\left\{ {{\begin{matrix} 1 & \ldots & {\overset{->}{x} = {\langle\;\rangle}} \\ 0 & \ldots & {\overset{->}{x} \neq {\langle\;\rangle}} \end{matrix}0}:\left. \overset{->}{x}\mapsto 0 \right.} \right. \right.$

On classical multiset relations (where all multiplicities are ≧0 and all tuples with multiplicity >0 have the same schema), * is the usual multiset natural join operation.

Given the three gmrs of A_(Rel)

$\begin{matrix} \begin{matrix} R & A & B & \; & \; & S & C & \; & T & B & C & \; & \; \\ \; & a_{1} & \; & \mapsto & r_{1} & \; & c & \mapsto & s & \; & c & \mapsto & t_{1} \\ \; & a_{2} & b & \mapsto & r_{2} & \; & \; & \; & \; & b & c & \mapsto & t_{2} \end{matrix} & {{Example}\mspace{14mu} 1} \end{matrix}$

over column name vocabulary Σ={A, B ,C} and value domain adorn={a₁,a₂,b,c}. For example, in multiset relation R, two tuples of different schema have a multiplicity other than 0. These two tuples can be specified as partial functions Σ→adorn: {A

a₁} and {A

a₂; B

b}.

Then S+T and R*(S+T) are as follows:

$\begin{matrix} {S + T} & B & C & \; \\ \; & \; & c & {{->{s + t_{l}}}} \\ \; & b & c & {{->t_{2}}} \end{matrix}$ $\begin{matrix} {R*\left( {S + T} \right)} & A & B & C & \; & \; \\ \; & a_{1} & \; & c & \mapsto & {r_{1}*\left( {s + t_{1}} \right)} \\ \; & a_{1} & b & c & \mapsto & {r_{1}*t_{2}} \\ \; & a_{2} & b & c & \mapsto & {{r_{2}*\left( {s + t_{1}} \right)} + {r_{2}*t_{2}}} \end{matrix}$

The missing values should not be taken as SQL null values, and * is not an outer join. (A_(Rel),+,*,0,1) is a commutative ring with identity.

AGCA defines two forms of query expressions, formulae and terms. Formulae evaluate to elements of the ring (A_(Rel),+,*,0,1) of gmrs. Terms evaluate to elements of the ring (A,+,*,0,1). Terms and formulae are connected mutually recursively, creating a powerful language for expressing aggregate queries. Both formulae and terms, and thus the overall query language, inherit the key properties of polynomial rings in that they have an additive inverse, a normal point of polynomial expressions, and admit a form of factorization. These properties are the basis of delta processing and incremental query evaluation in subsequent sections. Formulae are expressions built from atomic formulae using +, −, and *. The atomic formulae are true, false, relational atoms R(i) where 5C is a tuple of variables, and atomic conditions of the form t θ 0 comparing term t with 0 using comparison operations θ from=, ≠, >, ≧, <, and ≦. AGCA terms are built from variables, built-in function calls (constants are functions with zero arguments), and aggregate sums (Sum) using addition, its inverse, and multiplication. Built-in functions compute their result entirely based on their input terms, not accessing the database. The abstract syntax of formulae φ and terms t (given variables x, relation names R, comparison operators θ, and constants/builtin functions f) can be given by the EBNF

φ::−φ*φ|φ+φ|−φ|true|false|R({right arrow over (x)})|tθ0

t::−t*t|t+t|−t|f(t*)|x|Sum(t,φ)

x θ y can be written as (x−y)θ0. Formulae φ*ψ*π can be read as (φ*ψ)*π. The formal semantics of AGCA is given by mutually recursive functions [[•]]_(F)(•,•) and [[•]]_(T)(•,•) for formulae and terms, respectively. Given database A and a bound variable tuple {right arrow over (b)}(i.e., a function that maps each bound variable to a value), [[φ]]_(F)(A,{right arrow over (b)}) evaluates to an element of A_(Rel) and [[t]]_(T)(A,{right arrow over (b)}) evaluates to a value from A. The semantics of AGCA formulae is defined as follows.

$\begin{matrix} {\mspace{79mu} {{\left\lbrack \left\lbrack {\phi + \psi} \right\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)}:={{{\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)} +^{A_{Rel}}{\left\lbrack \lbrack\psi\rbrack \right\rbrack_{F}{\left( {A,\overset{->}{b}} \right)\mspace{79mu}\left\lbrack \left\lbrack {- \phi} \right\rbrack \right\rbrack}_{F}\left( {A,\overset{->}{b}} \right)}}:={{{-^{A_{Rel}}\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}}{\left( {A,\overset{->}{b}} \right)\mspace{79mu}\left\lbrack \left\lbrack {\phi*\psi} \right\rbrack \right\rbrack}_{F}\left( {A,\overset{->}{b}} \right)}:={\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)*^{A_{Rel}}\left\lbrack \lbrack\psi\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)}}}}} & \; \\ {\mspace{79mu} {{\left\lbrack \lbrack{true}\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)}:={{{1^{A_{Rel}}\mspace{79mu}\left\lbrack \lbrack{false}\rbrack \right\rbrack}_{F}\left( {A,\overset{->}{b}} \right)}:=0^{A_{Rel}}}}} & \; \\ {{\left\lbrack \left\lbrack {\phi*\left( {{x - y} = 0} \right)} \right\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)}:=\left. \overset{->}{x}\mapsto\left\{ \begin{matrix} {{\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)\left( \overset{->}{x} \right)} +} & \; & \; \\ {{\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)\left( {\overset{->}{x}_{{{dom}(\overset{->}{x})}\backslash {\{ y\}}}} \right)} +} & \; & \; \\ {\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)\left( {\overset{->}{x}_{{{dom}{(\overset{->}{x})}}\backslash {\{ x\}}}} \right)} & \ldots & {\left\lbrack \lbrack t\rbrack \right\rbrack_{T}\left( {A,{\overset{->}{b}\bigcup\overset{->}{x}}} \right){\theta 0}} \\ \; & \; & {{{and}\mspace{14mu} \left\{ \overset{->}{b} \right\} \left\{ \overset{->}{x} \right\}} \neq Ø} \\ 0 & \ldots & {otherwise} \end{matrix} \right. \right.} & \; \\ \left. {{{{\left\lbrack \left\lbrack {\phi*\left( {t\; {\theta 0}} \right)} \right\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)}:=\left( \; {{{*t\; {\theta 0}\mspace{14mu} {is}\mspace{14mu} {not}\mspace{14mu} {of}\mspace{14mu} {form}\mspace{14mu} x} - y} = 0} \right.}{*)}}\overset{->}{x}}\mapsto\left\{ \begin{matrix} {\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,\overset{\rightarrow}{b}} \right)\left( \overset{\rightarrow}{x} \right)} & \ldots & {\left\lbrack \lbrack t\rbrack \right\rbrack_{T}\left( {A,{\overset{->}{b}\bigcup\overset{->}{x}}} \right){\theta 0}} \\ \; & \; & {{{and}\mspace{14mu} \left\{ \overset{->}{b} \right\} \left\{ \overset{->}{x} \right\}} \neq Ø} \\ 0 & \ldots & {otherwise} \end{matrix} \right. \right. & \; \\ {{\left\lbrack \left\lbrack {R\left( {x_{1},\ldots \mspace{14mu},x_{k}} \right)} \right\rbrack \right\rbrack_{F}\left( {A,\overset{->}{b}} \right)}:=\left. \overset{->}{x}\mapsto\left\{ \begin{matrix} {R^{A}\left( \overset{->}{y} \right)} & \ldots & {{{\left\{ \overset{->}{b} \right\} \left\{ \overset{->}{x} \right\}} \neq Ø},} \\ \; & \; & {{{{dom}\left( \overset{->}{x} \right)} = \left\{ {x_{1},\ldots \mspace{14mu},x_{k}} \right\}},} \\ \; & \; & {{{{sch}(R)} = {\langle{A_{1},\ldots \mspace{14mu},A_{k}}\rangle}},{and}} \\ \; & \; & {\overset{->}{y} = \left\{ {\left. A_{i}\mapsto{\overset{->}{x}\left( x_{i} \right)} \right.{1 \leq i \leq k}} \right\}} \\ 0 & \ldots & {{otherwise}.} \end{matrix} \right. \right.} & \; \end{matrix}$

Typed tuples are functions: {{right arrow over (b)}}

{{right arrow over (x)}}≠Ø asserts that the typed tuples {right arrow over (b)} and {right arrow over (x)} are consistent, that is, for all y ∈ dom({right arrow over (b)})∩dom({right arrow over (x)}), {right arrow over (b)}(y)={right arrow over (x)}(y). The union {right arrow over (b)}∩{right arrow over (x)} extends one typed tuple by the other. The definition of [[R(x₁, . . . ,x_(k))]]_(F) supports column renaming. For example, if R is a database relation

$\begin{matrix} R^{A} & a & b & \; & \; \\ \; & a_{1} & b_{1} & \mapsto & r_{1} \\ \; & a_{2} & b_{2} & \mapsto & r_{2} \end{matrix}$ then $\begin{matrix} {\left\lbrack \left\lbrack {R\left( {x,y} \right)} \right\rbrack \right\rbrack_{F}\left( {A,\left\{ y\mapsto b_{1} \right\}} \right)} & x & y & \; & \; \\ \; & a_{1} & b_{1} & \mapsto & r_{1} \end{matrix}$

The query renames the columns (a ,b) to (x, y) and selects on y since it is a bound variable.

For example, if a₁, . . . ,a₅ ∈ A=Z and

$\begin{matrix} {\left\lbrack \lbrack\phi\rbrack \right\rbrack_{F}\left( {A,Ø} \right)} & x & y & \; & \; \\ \; & 1 & \; & \mapsto & a_{1} \\ \; & \; & 1 & \mapsto & a_{2} \\ \; & 1 & 1 & \mapsto & a_{3} \\ \; & 1 & 2 & \mapsto & a_{4} \end{matrix}$ then $\begin{matrix} {\left\lbrack \left\lbrack {\phi*\left( {x < y} \right)} \right\rbrack \right\rbrack_{F}\left( {A,Ø} \right)} & x & y & \; & \; \\ \; & 1 & 2 & \mapsto & a_{4} \end{matrix}$ $\begin{matrix} {\left\lbrack \left\lbrack {\phi*\left( {x = y} \right)} \right\rbrack \right\rbrack_{F}\left( {A,Ø} \right)} & x & y & \; & \; \\ \; & 1 & 1 & \mapsto & {a_{1} + a_{2} + a_{3}} \end{matrix}$

For example, for {right arrow over (x)}={x

1; y

1} and {right arrow over (b)}Ø, [[x−y]]_(T)(A,{right arrow over (b)}∪{right arrow over (x)})=[[x]]_(T)(A,{right arrow over (b)}∪{right arrow over (x)})−[[y]]_(T)(A,{right arrow over (b)}∪{right arrow over (x)})=1−1=0 and {{right arrow over (b)}}

{{right arrow over (x)}}={{right arrow over (x)}}, thus

[[φ*(x=y)]]_(F)(A,{right arrow over (b)})({right arrow over (x)})=[[φ]]_(F)(A,{right arrow over (b)})({x

1})+[[φ]]_(F)(A,{right arrow over (b)})({y

1})+[[φ]]_(F)(A,{right arrow over (b)})({right arrow over (x)})=a ₁ +a ₂ +a ₃.

The semantics of AGCA terms is defined as

$\begin{matrix} {\mspace{79mu} {{\left\lbrack \lbrack x\rbrack \right\rbrack_{T}\left( {A,\overset{->}{b}} \right)}:=\left\{ \begin{matrix} {\overset{->}{b}(x)} & \ldots & {x \in {{dom}\left( \overset{->}{b} \right)}} \\ {fail} & \ldots & {otherwise} \end{matrix} \right.}} & \; \\ {\mspace{79mu} {{\left\lbrack \left\lbrack {f\left( {t_{1},\ldots \mspace{14mu},{tk}} \right)} \right\rbrack \right\rbrack_{T}\left( {A,\overset{->}{b}} \right)}:={f\left( {{\left\lbrack \left\lbrack t_{1} \right\rbrack \right\rbrack_{T}\left( {A,\overset{->}{b}} \right)},\ldots \mspace{14mu},{\left\lbrack \left\lbrack t_{k} \right\rbrack \right\rbrack_{T}\left( {A,\overset{->}{b}} \right)}} \right)}}} & \; \\ {{\left\lbrack \left\lbrack {{Sum}\left( {t,\phi} \right)} \right\rbrack \right\rbrack_{T}\left( {A,\overset{->}{b}} \right)}:={\sum\limits_{{{({\overset{->}{x}\mapsto a})} \in {{\lbrack{\lbrack\phi\rbrack}\rbrack}_{F}{({A,\overset{->}{b}})}}},{{{\{\overset{->}{b}\}}{\{\overset{->}{x}\}}} \neq Ø}}{a*^{A}\left\lbrack \lbrack t\rbrack \right\rbrack_{T}\left( {A,{\overset{->}{b}\bigcup\overset{->}{x}}} \right)}}} & \; \end{matrix}$

Take +, −, and * as binary built-in functions f (+^(A), −^(A), and *^(A)) and constants as nullary built-in functions and the definition is complete. For example, if gmr R of the previous example is,

$\begin{matrix} {{\left\lbrack \left\lbrack {{Sum}\left( {{3*x},{R\left( {x,y} \right)}} \right)} \right\rbrack \right\rbrack \left( {A,Ø} \right)} = {{r_{1}*\left\lbrack \left\lbrack {3*x} \right\rbrack \right\rbrack_{T}\left( {A,\left\{ x\mapsto a_{1} \right\}} \right)} +}} \\ {{r_{2}*\left\lbrack \left\lbrack {3*x} \right\rbrack \right\rbrack_{T}\left( {A,\left\{ x\mapsto a_{2} \right\}} \right)}} \\ {= {{r_{1}*3*a_{1}} + {r_{2}*3*{a_{2}.}}}} \end{matrix}$

Note that [[•]]_(F) maps to elements of A_(Rel); in particular, the image elements are functions Tup→A that are nonzero on at most a finite number of inputs. This is ensured by syntactically requiring each condition to be part of a product φ*(tθ0) with another formula φ that guards its domain. (In the case that a condition tθ0 is not the second argument of a multiplication, read it as formula “true*(tθ0)”.) The evaluation of variables x using [[x]]_(T) can fail if they are not bound at evaluation time. Illegal queries can be excluded from AGCA. A definition of range-restriction completely analogous to that for relational calculus, with A and v replaced by * and +, respectively, allows to statically verify that such runtime failures do not happen. A SQL aggregate query

-   SELECT {right arrow over (b)}, SUM(t) FROM R₁ r₁₁, R₁ r₁₂, . . . ,     R₂ r₂₁, . . . -   WHERE φ GROUP BY {right arrow over (b)} -   is expressed in AGCA as

Sum(t,R₁({right arrow over (x)}₁₁)*R₁({right arrow over (x)}₁₂)* . . . *R₂({right arrow over (x)}₂₁)* . . . *φ)

with bound variables {right arrow over (b)}. While Sum(•,•) computes exactly one number, we can think of an SQL aggregate query with group by clause as a function {right arrow over (b)}

[[Sum(•,•)]](A,{right arrow over (b)}) that maps each group to its aggregate value. For example, relation C(cid, nation) stores the ids and nationalities of customers. The SQL query

-   SELECT C1.cid, SUM(1) FROM C C1, C C2 -   WHERE C1.nation=C2.nation -   GROUP BY C1.cid; -   asks, for each cid, for the number of customers of the same nation     (including the customer identified by cid). This query translates to     AGCA as

Sum(1,C(c ₁ ,n ₁)*C(c ₂ , n ₂)*(n ₁ =n ₂))

with bound variable c₁. A language of expressions can be constructed from values (”constants“) and the operations + and * of a ring A, plus variables. If these expressions evaluate to elements of a multivariate polynomial ring in the natural way, turning an expression into a polynomial, that is, a sum of flat products (the products are also known as monomials), means applying distributivity repeatedly until the result is a polynomial. This can be combined with simplification operations based on the 1 and 0-elements and the additive inverse, i.e., α*1 maps to α,α*0 maps to 0, α+0 maps to α, α+(−α) maps to 0 , −(−α)=α, and (−α)*β=−(α*β). Such an algorithm for computing and simplifying expressions over a ring is applicable to AGCA formulae and terms. In particular, the operation * is distributive with + despite its sideways information passing semantics. For arbitrary terms s and t and formulae φ and ψ, Sum terms can be simplified using the following equations (to be applied by replacing a left by a right hand side expression):

Sum(s+t,φ)=Sum(s,φ)+Sum(t,φ)

Sum(t,φ+ψ)=Sum(t,φ)+Sum(t,ψ)

Sum(−t,φ)=−Sum(t,φ)

Sum(t,−φ)=−Sum(t,φ)

Sum(t,true)=t

Sum(t,false)=0

Sum(0,φ)=0.

A term that contains neither + nor − anywhere is referred to as recursively monomial. The following result is based on an algorithm that rewrites an input term in a bottom-up pass using the above rules. Each AGCA term is equivalent to a finite sum±t₁±t₂ . . . ±t_(m) where t₁, . . . , t_(m) are recursively monomial terms. For example, Sum(t, (−φ)*((−ψ)+π)) simplifies to the recursively monomial Sum(t,φ*ψ)−Sum(t,φ*π). When e is either a formula or a term, vars(e) be the set of all variables occurring in e. Factorization employs the equivalence

Sum(s*t,φ*ψ)=Sum(s,φ)*Sum(t,ψ)

which is true if (vars(s)∪vars(φ))∩(vars(t)∪vars(ψ))=Ø. A monomial aggregate term can be maximally factorized in linear time in its size. For example , the term Sum(5*x*Sum(1,R(y, z))*w,R(x, y)*R(v,w)) factorizes as Sum(5,true)*Sum(x*Sum(1,R(y, z)), R(x, y))*Sum(w,R(v,w)). Recursive factorization, given term Sum(t,φ), recursively, for example, from the bottom-up, factorizes the aggregate terms in t before applying factorization as just described to Sum(t,φ) itself. Unneeded variables can be eliminated from queries, variables that are neither bound nor do they occur in relation atoms, but obtain their values through condition atoms that equate them with other variables. For a AGCA term Sum(t,φ) and a set of bound variables B, then there is an equivalent term Sum(t′,v) such that each variable in ψ either occurs in a relational atom R({right arrow over (x)}) of ψ or in B. Given an arbitrary monomial formula φ with variables vars(φ) and a set of bound variables B, there is a monomial formula ψ with variables vars(ψ) such that each variable in vars(ψ) either occurs in a relational atom R({right arrow over (x)}) of ψ or in B and ∃(vars(φ)−B)φ is equivalent to ∃(vars(ψ)−B)ψ. Variables are eliminated that occur in constraint atoms x θ y but neither in relational atoms nor in B. If φ=Λ(E∪O), where E is the set of equality atoms x=y and O is the set of remaining atoms (either set may be empty), and a set of bound variables, redundant variables are eliminated as follows. For each equivalence class C of the equivalence relation E, an element (i.e., variable) can be distinguished as x_(C) such that, if B∩C≠Ø, x_(C) is an arbitrary element of B∩C; otherwise, it is an arbitrary element of C. A unification mapping Θ can be created that maps each unbound variable y of E to x_(|y|) (where [y] is the equivalence class of y) and is the identity on the bound variables. Variables in O can be substituted using Θ, obtaining O′. If E′=∪{y=x_(|y|)|y ∈ ((B∩[y])−{x_(|y|)})}, then φ can be replaced by (ΛO′)

ΛE′. This can eliminate redundant variables involved in equalities. Variables involved in inequality constraints can occur either in B or in a relational atom, otherwise φ has to be unsafe.

In the case where Sum(t,φ) (where φ is a monomial) and bound variables are defined, variables can be eliminated by first eliminating variables in φ, creating φ′ and Θ. Then variables of t can be substituted that are in the domain of Θ using Θ, obtaining t′. The result, Sum(t′,φ′), is equivalent to Sum(t,φ).

In the language of the present embodiment, N0C, for incremental update-event processing that admits massive parallelization, state is represented by finite map data structures (associative arrays), mapping tuples to numbers. NC0C can be a restricted imperative language, similar to C both in syntax and semantics. There are at least two differences between NC0C and C. The first is in the initialization of undefined map values, which uses special syntax to account for the fact that NC0C procedures are update triggers for the map data structures. The second is in looping, which is performed over a set of values that is intentionally not made explicit in the syntax of NC0C (for example, it can be a form of constrained structural recursion over the map—and thus can admit parallelization). AGCA queries can be compiled to NC0C programs that perform incremental view maintenance. These compiled programs only use the maps to represent the view and auxiliary data; no database beyond these is accessed. A map (read) access, where m is a map name, is written as m[{right arrow over (x)}]. An NC0C term is an arithmetic expression built from variables, constants, map accesses, functional ifs of the form if φ then t else 0 or, using C syntax, (φ?t: 0), and arithmetic operations + and *. A condition φ is a Boolean combination of (in)equalities over variables and constants. An NC0C trigger is of the form

on±R({right arrow over (x)}{right arrow over (y)}){s₁; . . . ; s_(k)}

where ± indicates insertion respectively deletion, R is a relation name, {right arrow over (x)}{right arrow over (y)} are variables (the trigger arguments), and s₁, . . . ,s_(k) are NC0C statements of the form

for each {right arrow over (z)} do m[{right arrow over (x)}{right arrow over (z)}]<t _(init) >±=t

where {right arrow over (z)} are variables distinct from {right arrow over (x)}{right arrow over (y)}, t is an NC0C term, and t_(init) is an NC0C term without map accesses that uses only variables from {right arrow over (x)}{right arrow over (z)}, called the initializer of m[{right arrow over (x)}{right arrow over (z)}]. If t_(init) is 0, it can be omitted and m[{right arrow over (x)}{right arrow over (z)}] can be written rather than m[{right arrow over (x)}{right arrow over (z)}]<0>. If m₁[{right arrow over (v)}₁], . . . , m_(k)[{right arrow over (v)}_(k)] are the map accesses in the right-hand side term t, then m, m₁, . . . , m_(k) must be pairwise distinct and the variables in {right arrow over (v)}₁, . . . , {right arrow over (v)}_(k) must be a nonoverlapping subsets of the variables in {right arrow over (x)}{right arrow over (y)}{right arrow over (z)}. Statements of the form (1) can be abbreviated with {right arrow over (z)}=<> as

m[{right arrow over (x)}]<t_(init) >±t.

An NC0C program can include of a set of triggers, one for each update event ±R . The semantics of NC0C terms can be, for example, the same as in C. A statement of form (1) can perform the following for each valuation θ of the variables {right arrow over (z)} (extending the valuation of variables {right arrow over (x)}{right arrow over (y)} passed to the trigger via its arguments) such that all map accesses in right-hand side t are defined. If m[{right arrow over (x)}{right arrow over (z)}] is undefined, it can be initialized with t_(init). Then, unconditionally, m[{right arrow over (x)}{right arrow over (z)}]+=t can be executed. An “on ±R({right arrow over (x)}{right arrow over (y)})” trigger fires if the update is of the form ±R({right arrow over (x)}{right arrow over (y)}) and executes the statements s₁; . . . ; s_(k) of its body sequentially (as in C). Compilation algorithms can create multiple triggers for the same update event. However, NC0C programs do not have cyclic dependencies between triggers: there is no trigger that reads one map that the other updates and vice-versa (there is a hierarchy of maps). Assuming without loss of generality that the argument variable tuples of distinct triggers for the same update event are the same, a topological sort of the triggers can be performed that assures that no map is read after it is written, and their bodies can be concatenated according to this sort to obtain a single trigger per update event. An exemplary NC0C on-insert trigger follows:

on +C(cid, nation) { q[cid] += q1[nation]; foreach cid2 do q[cid2] += q2[cid2, nation]; q[cid] += 1; q1[nation] += 1; q2[cid, nation] += 1 }

The initializers are all 0, and are thus omitted. The trigger on −C is obtained by changing += to −= everywhere in the above trigger except for the third statement (q[cid]+=1), which remains unchanged.

FIG. 2 is a table of a trace of map q that changes during a sequence of insertions and deletions. The Δq[x] columns indicate the changes made to q[x] on each update. Methods for compiling AGCA queries to exemplary language NC0C can be given. AGCA sum terms excluding nested aggregates and inequality join conditions (i.e., involving two variables, e.g. x<y ; non-join conditions such as x>5 are permitted) are known herein as primitive AGCA terms. NC0C initializers can be, but are not required to be, constants so as going to the database to compute is not necessary. An AGCA term t=Sum(t₀,φ) is called constraints-only if φ does not contain relational atoms R({right arrow over (x)}). When φ contains only bound variables, t can be a (functional) if-statement “if φ then t₀ else 0” or, using C syntax, “φ ? t₀: 0”. MakeC (t,B) can be a function that turns t into the corresponding functional if-statement after performing variable elimination using bound variables B. In the present embodiment there is a compilation method for AGCA terms Sum(t, φ) that does not contain nested aggregates, i.e., neither t nor φ contain Sum terms. In the present embodiment, any primitive AGCA term can be compiled into an NC0C program that incrementally maintains it. To create on-insert (+R) and on-delete (−R) triggers that incrementally maintain map q[{right arrow over (b)}] of Sum term t, the following method can be executed as Compile0(q, {right arrow over (b)}, t):

Compile0( q , {right arrow over (b)} , t ) outputs an NC0C program   begin   for each relation R in the schema, ± in {+,−} do    {right arrow over (a)} := turn sch(R) into a list of new variable names;    t_(init) := [[t]]_(T)(Ø,{right arrow over (a)}{right arrow over (b)});    t′ := Δ_(±R(){right arrow over (a)}₎t ;   if t′ is constraints only then t″ := MakeC( t′ , {right arrow over (a)}{right arrow over (b)} )   else t″ := q_(±R)[{right arrow over (a)}{right arrow over (b)}]; Compile0( q_(±R), {right arrow over (a)}{right arrow over (b)} , t′ ) end if;   output on ±R({right arrow over (a)}) {foreach {right arrow over (b)} do q[{right arrow over (b)}] < t_(init) > += t″ } end q±R is a new map name for an auxiliary materialized view. When {right arrow over (b)} is the empty tuple, “for each {right arrow over (b)} do” can be omitted from the NC0C statement. The method of the present embodiment takes an aggregate query t=Sum(t₀, φ) with bound variables {right arrow over (b)} and defines a map q[{right arrow over (b)}] for it, representing a materialized view of the query. The method can create a trigger for each possible update event ±R which specifies how to update q[{right arrow over (b)}] when this event occurs. To create a trigger, the method can compute the delta t′ of the query, and can create a new map q±R representing a materialized view of the delta. The statement increments q[{right arrow over (b)}] by q±R[•], and uses the result of evaluating term t on the empty database as the initializer for q[{right arrow over (b)}]. In particular, on Sum terms t that are not constraints-only, t_(init)=0. The new map q±R is incrementally maintained as well. To do this, the method recursively calls itself and terminates because the delta t′ eventually reaches degree 0 (i.e., is constraints-only). In this case no new map is created for it but t′ (turned into an NC0C term using MakeC) itself is used as the right-hand side of the NC0C statement. For example, if q[]=Sum(1,R(x)*S(x)), then

Δ_(±R(u)) q=±Sum(1,(x=u)*S(x))=:q_(R) [u]

Δ_(±S(v)) q=±Sum(1,R(x)*(x=v))=:q _(S) [v]

Δ_(±S(v)) q _(R) [u]=±Sum(1,(x=u)*(x=v))

Δ_(±R(u)) q _(S) [v]=±Sum(1,(x=u)*(x=v))

If MakeC (Sum(1,(x=u)*(x=v)))=if (u−v) then 1 else 0. Compile0 produced the NC0C insert triggers

on+R(u){q[]+=qR[u]}

on+R(u) {for each v do

qS[v]+=if(u=v) then 1 else 0}

on+S(v) {q[]+=qS[v]}

on+S(v) {for each u

do qR[u]+=if(u=v) then 1 else 0

The delete triggers are obtained from the insert triggers by replacing all occurrences of + by −,

To loop over fewer variables, aggregates can be extracted. For a term t and its set B of bound variables, the function Extract (t, B) can replace each maximal subterm s for t that is of the form Sum(•,•) but is not constraints-only, by a map access m[{right arrow over (x)}]. m is a new name and {right arrow over (x)} are those variables of B that occur in s, turned into an arbitrarily ordered tuple. The results of Extract is a pair (t′, Θ) of the remainder term t′ and a mapping Θ from map accesses m[{right arrow over (x)}] to extracted subterms s which could be used to undo the extraction. That is, t′ is constraints-only and t′ with its map accesses substituted using Θ is t. For example, if t is the term Sum(x*Sum(v, R(v, z)), y=z)*Sum(u,R(x,u)). extract (t,{x, y}) returns the pair (t′, θ) including term t′=Sum(x*m₁[z]<θ(m₁[z])>,y=z)*m₂[x])<θ(m₂[x])> and the mapping

θ={m ₁ [z]

Sum(v,R(v,z)); m ₂ [x]

Sum(u,R(x,u))}.

If ψ is a constraints-only term in which all variables are bound and t₀ is a term in which all variables are bound, then

Sum(t,φ,ψ)=Sum(Sum(t,φ),ψ)

t₀*Sum(t,ψ)=Sum(t ₀ *tψ)

Thus, given a recursively monomial term, ψ can be lifted to the top. For example,

LiftIfs(Sum(1,C(c ₂ , n)*(c ₁ =c)),{c ₁ ,c,n})=

Sum(Sum(1,C(c ₂ , n)), c ₁ =c).

To address further auxiliary functions, Simplify (t, B), given an aggregate term t and a set of bound variables B,

-   turns t into an equivalent sum of (inverses of) recursive monomials,     recursively factorizes each of the result monomials, eliminates all     variables others than those in B or a relational atom, and performs     LiftIfs(•, B).

The result t±₁t₁ . . . ±_(n)t_(n) is equivalent to t and the t, are recursive monomials involving only variables in B.

EIimLV is a function that takes an NC0C statement

foreach {right arrow over (x)}{right arrow over (y)} do q[{right arrow over (x)}{right arrow over (y)}]+=if {right arrow over (x)}={right arrow over (z)} then t else 0

and simplifies it to the equivalent statement

foreach {right arrow over (y)} do q[{right arrow over (z)}{right arrow over (y)}]+=t.

The method Compile(m, {right arrow over (b)}, t), outputing an NC0C program, can be invoked in a similar way to Compile0 except that the delta of the query is first simplified and the non-constraints-only aggregates are extracted.

Compile( m , {right arrow over (b)} , t ) begin for each relation R in the schema, ±₀ in {+,−} dō  {right arrow over (a)} := turn sch(R) into a list of new variable names;  t_(init) := [[t]]_(T)(Ø,{right arrow over (a)}{right arrow over (b)});  t′ := Δ_(±) ₀ _(R(){right arrow over (a)}₎t ;  ( ±₁t₁...±_(n)t_(n), Θ ) := Extract(Simplify( t′ , {right arrow over (a)}{right arrow over (b)} ), {right arrow over (a)}{right arrow over (b)} );  foreach i from 1 to n    s_(i) := (foreach {right arrow over (b)} do m[{right arrow over (b)}] < t_(init) > (±_(i))= MakeC( t_(i), {right arrow over (a)}{right arrow over (b)} ));    output on ±₀R({right arrow over (a)}) { ElimLV(s_(i)) };  foreach (m’[{right arrow over (x)}] 

t″) in Θ do Compile( m′,{right arrow over (x)},t″ ); end

The result is a sum of constraints-only recursive monomials with map accesses. Each of the recursive monomials becomes a separates statement because, using Liftlfs and ElimLV, the loop variables are removed, and each of the statements (monomials) can loop over a different subset of the argument variables of the map representing the query (the remaining variables are substituted by constants). Given a primitive term t and bound variables {right arrow over (x)} by which results are to be grouped, the output of Compile(m, {right arrow over (x)}, t) is an NC0C program that can maintain query t in map m[{right arrow over (x)}] under inserts and deletes. For example, Compile can simplify the two foreach-loop statements of the triggers given previously to qS[u]+=1 and qR[v]+=1. The resulting triggers have no loops and run in constant sequential time. For example, an AGCA query compilation can be invoked as follows:

Compile(q,{c ₁},Sum(1,C(c ₁ ,n ₁)*C(c ₂ ,n ₂)*(n ₁ =n ₂))).

A sum of (inverses of) recursive monomials equivalent to Δ_(±C(c,n))q[c₁] has been previously computed. Factorization on that query is the identy. Eliminating variables with variables (c₁,c,n) bound yields ±Sum(1,(c₁=c)*C(c₂,n))±Sum(1,C(c₁,n))+Sum(1,c₁=c). The result of if-lifting for the first of the three monomials is

LiftIfs(Sum(1,C(c ₂ ,n)*(c ₂ =c)),{c ₁ ,c,q)=

sum(sum(1,C(c ₂ ,n)),c ₁ =c).

For the other two monomials, variables elimination is the identity.

Extract(Simplify(Δ_(±C(c,n)) q[c ₁ ,{c ₁ ,c,n}),{c ₁ ,c,n})

yields (±t₁±t₂+t₃,Θ) where t₁=Sum(q1[n],c₁=c), t₂=q2[c₁, n], t₃=Sum(1,c₁=c), and

$\Theta = \begin{Bmatrix} {q\; {1\lbrack n\rbrack}} & \mapsto & {{Sum}\left( {1,{C\left( {c_{2},n} \right)}} \right)} \\ {q\; {2\left\lbrack {c_{1},n} \right\rbrack}} & \mapsto & {{{Sum}\left( {1,{C\left( {c_{1},n} \right)}} \right)}.} \end{Bmatrix}$

The statements s₁,s₂,s₃ are

-   foreach c₁ do q[c₁]±=if c₁=c then q1[n] else 0; -   foreach c₁ do q[c₁]±=q2[c₁, n]; -   foreach c₁ do q[c₁]±=if c₁=c then 1 else 0 -   ElimLV can simplify these statements, and the resulting triggers are

on±C(c,n){q[c]±=q1[n] else 0 }

on±C(c,n){foreach c ₁ do q[c ₁ ]±=q2[c ₁ , n]}

on±C(c,n){q[c]±=1}

q1 and q2 can be compiled. Since

Δ_(±C() c′,n′)q1[n]=Δ _(±C)(c′,n′)q2[c,n]=±1,

The compiled NC0C program after merging triggers as described previously is

on +C(cid, nation) { q[cid] += q1[nation]; foreach cid2 do q[cid2] += q2[cid2, nation]; q[cid] += 1; q1[nation] += 1; q2[cid, nation] += 1 }

Compilable AGCA terms are those without nested aggregates or inequality join conditions. The second requirement of that definition can be replaced by the requirement to exclude terms that are unsafe if the set of bound variables is set to Ø or where this condition can become true for a k-th delta. For example, the query q[]=Sum(1,R(x)*S(y*(x<y)) can be excluded because its delis m[y]=Sum(1,R(x)*(x <y)) is safe for bound variable y, but unsafe for the empty set of bound variables. On an insertion into R, it is not known for which y values the map m should be updated. If it a global, immutable active domain is assumed, it is not necessary to compute initial values, and the compilation methods can be applicable to all Sum terms with simple conditions, i.e. for which taking deltas simplifies the query structure. For primitive queries, for the initialization of a map value, m[{right arrow over (a)}]<t_(init)>, t_(init) can be evaluated on the empty database, and contents of the database are not visible to query t_(init) or would otherwise have caused initialization of m[{right arrow over (a)}] earlier.

Referring now to FIG. 3, system 100 of the present embodiment can include, but is not limited to including, query compiler 13 which can include, but is not limited to including core data structures 15, a conventional SQL parser 17 such as, for example, GSP's SQL parser, compilation algorithm 19 and query transformation library 21. System 100 receives query 11 and produces query source code 22, which implements a query runtime 23, that is received by conventional source code compiler 27, which produces compiled binary 29. Core data structures 15 can include, but are not limited to including, ring 15A, hypergraph 15B, NC0C abstract syntax tree (AST) 15D, a data structure to describe NC0C programs as presented above, and a standard query AST 15C, a conventional data structure that describes queries as produced by the conventional SQL parser. A hypergraph is a pair (V, E) where V is a set “of nodes” and E is a set of subsets of V. Thus, a hypergraph generalizes a graph in that edges become so-called “hyperedges” (the members of E), which connect possibly more or fewer than two nodes with each other. SQL Parser 17 can receive, but is not limited to receiving, query 11 and information from core data structures 15 to provide initial query AST 18 to compilation algorithm 19. Compilation algorithm 19 can include, but is not limited to including, initializer 19A, elimLV 19B, extract 19C, and MakeC and Output 19D. Compilation algorithm 19 can receive information from core data structures 15, initial query AST 18, and delta queries 20 which have undergone variable elimination and factorization and can provide delta queries 20 to query transformation library 21, and also can provide query source code 22 that implements query runtime 23. Initializer 19A, elimLV 19B, extract 19C, and MakeC and Output 19D have been described in detail previously. Query transformation library 21 can include, but is not limited to including, delta computation 21 A and simplify 21B, also described in detail previously. Query transformation library 21 can receive information from core data structures 15, and can provide delta queries 20 to compilation algorithm 19. Query runtime 23 can include, but is not limited to, map definitions 23A, which are source code declarations for maps, and thus materialized views, being maintained by NC0C program 23B as described above, as received in the query source code 22 from compilation algorithm 19, and can be provided to a conventional source code compiler 25 to yield a compiled binary 27.

Referring now to FIG. 4, method 150 executed by query compiler 13 of the present embodiment can include, but is not limited to including, the steps of receiving map name m, map arguments x, and query t, determining 51 relations R in query t, assigning 55 names a to trigger arguments based on the schema of R, if 53 there are more relations R, creating 57 initial value query t_(init), for query t based on trigger arguments a and map arguments x, computing 59 delta query t′ based on query t, trigger arguments a and relation R, simplifying 61 delta query t based on trigger arguments a and map arguments x to produce simplified delta monomials t_(mi, extracting 63 aggregates based on simplified delta monomials t) _(init), trigger arguments a, and map arguments x to produce monomials t, with substitutions, map names, and defining queries, for each 65 t_(i) creating 67 map update statement s_(i based on map arguments x, map name m, initial value query t) _(init), delta query t′, and trigger arguments a, eliminating 69 map update loops to produce optimized update statement s′_(i), based on update statement s_(i) and storing 71 optimized update statement s′_(i), based on trigger arguments a and relation R, and recursively calling 73 query compiler 13 with arguments based on map name m, map arguments x, and delta delta query t″.

An alternate method for creating code to optimize queries of a database can include, but is not limited to including, the steps of (a) receiving a map name, map arguments, and a query, (b) determining relations in the query, (c) determining trigger arguments from each of the relations, (d) computing an initial value query based on an evaluation of the query, the map arguments, and the trigger arguments, the initial value query being computed by executing the query by a conventional query processor on an empty database, (e) computing a delta query based on the query, the relation, and the trigger arguments, the delta query being computed by conventional delta query computation for incremental view maintenance, (f) computing simplified delta monomials based on the delta query, the trigger arguments, and the map arguments by performing the steps of: (1) computing a polynomial version of the query, (2) applying factorization to each monomial of the polynomial, (3) eliminating from each of the monomials the trigger arguments and the map arguments that can be can be connected by equality to other of the trigger arguments and the map arguments, and (4) creating the simplified delta monomials from monomials in which the trigger arguments and the map arguments do not reference the database, (g) extracting aggregates as the monomials that reference the database based on the simplified delta monomials, the map arguments, and the trigger arguments, (h) computing a map update statement for each of the simplified delta monomials based on the simplified delta monomial, the map arguments, and the trigger arguments, (i) computing optimized update statement by eliminating map update loops from the map update statement for each revised simplified delta monomial based on the map update statement, the optimized update statement being computed by ElimLV, (j) generating code from the optimized update statements based on the relation and the trigger arguments, (k) repeating steps (a)-(k) when there are more of the relations in the query, (l) for each aggregate query in the delta query, recursively applying steps (a)-(m) to the aggregate query, the arguments including the trigger arguments remaining in the aggregate query after simplification, and (m) repeating steps and (a) through (m) when there are more of the aggregates.

Referring primarily to FIG. 4, method 150 can be, in whole or in part, implemented electronically. Signals representing actions taken by elements of system 100 (FIG. 3) can travel over electronic communications media and from node to node in a communications network. Control and data information can be electronically executed and stored on computer-readable media. Method 150 can be implemented to execute on a node in a computer communications network. Common forms of computer-usable media include, for example, a floppy disk, a flexible disk, hard disk, magnetic tape, or any other magnetic medium, a CDROM, any other optical medium, punched cards, paper tape, any other physical medium with patterns of holes, a RAM, a PROM, and EPROM, a FLASH-EPROM, thumb drive, any other memory chip or cartridge, or any other medium from which a computer can read. From a technological standpoint, a signal or carrier wave (such as used for Internet distribution of software) encoded with functional descriptive material is similar to a computer-readable medium encoded with functional descriptive material, in that they both create a functional interrelationship with a computer. In other words, a computer is able to execute the encoded functions, regardless of whether the format is a disk or a signal. System 100 (FIG. 3) can include a communications network that can include at least one node for carrying out method 150. System 100 (FIG. 3) can be a distributed network (such as a network utilizing a distributed system format such as, but not limited to, DCOM or CORBA) where computer data signal (such as, but not limited to, signals over the Internet) traveling over the communications network carrying information capable of causing a computer system in the communications network to practice method 150. The communications network can be a conventional network; the signal is modulated and supplemental data is embedded utilizing conventional means. System 100 (FIG. 3) can include a computer readable medium having instructions embodied therein for the practice of method 150. 

1. A method for incrementally maintaining a representation of the result of a computation uses k-th deltas of the specification, for k>1, comprising the steps of: materializing representations of the computation results of multiple levels of the specification; maintaining the materialized representations concurrently; maintaining the materialized representation of level k under updates using the specifications of a level greater than k; and performing the updates of the representation by the computation whose inputs are a state of the representation, update information related to the updates, and at least one of the representations of the specifications of a level greater than k.
 2. The method as in claim 1, where the specifications is a database query.
 3. The method of either of claim 2 further comprising the step of: compiling the database query.
 4. The method of either of claim 2 further comprising the step of: passing messages between a plurality of the representations to maintain a hierarchy of the representations and to update the representations.
 5. The method of either of claim 2 further comprising the step of: performing the computations on the updates by code obtained by compilation of the specification.
 6. The method of either of claim 2 further comprising the step of: performing the computations on at least one computer by machine code obtained by compilation of the specification.
 7. The method of either-of claim 2 further comprising the step of: performing the computations on hardware generated to execute machine code obtained by compilation of the specification.
 8. The method of claim 1 further comprising the step of: performing the computations on at least one Boolean circuit executing machine code obtained by compilation of the specification.
 9. The method of either of claim 2 wherein the representations of the computation results of specifications are relations.
 10. The method of either of claim 2 wherein the representations of the computation results of specifications are finite functions.
 11. The method of either of claim 2 wherein the representations of the computation results of specifications are hash tables.
 12. The method of either of claim 2 further comprising the step of: parallelizing incremental view maintenance.
 13. A method for creating code to optimize queries of a database comprising the steps of: (a) receiving a map name, map arguments, and a query; (b) determining relations in the query; (c) determining trigger arguments from each of the relations; (d) computing an initial value query based on an evaluation of the query, the map arguments, and the trigger argument; (e) computing a delta query based on the query, the relation, and the trigger arguments; (f) computing simplified delta monomials based on the delta query, the trigger arguments, and the map arguments by performing the steps of: (1) computing a polynomial version of the query; (2) applying factorization to each monomial of the polynomial; (3) eliminating from each of the monomials the trigger arguments and the map arguments that can be can be connected by equality to other of the trigger arguments and the map arguments; and (4) creating the simplified delta monomials from monomials in which the trigger arguments and the map arguments do not reference the database; (g) extracting aggregates as the monomials that reference the database based on the simplified delta monomials, the map arguments, and the trigger arguments; (h) computing a map update statement for each of the simplified delta monomials based on the simplified delta monomial, the map arguments, and the trigger arguments; (i) computing optimized update statement by eliminating map update loops from the map update statement for each revised simplified delta monomial based on the map update statement; (j) generating code from the optimized update statements based on the relation and the trigger arguments; (k) repeating steps (a)-(k) when there are more of the relations in the query; (l) for each of the aggregates, recursively applying steps (a)-(l) when there are more of the aggregates, the arguments for said step of recursively applying include the trigger arguments remaining in the aggregate after said step of creating the simplified delta monomials.
 14. A computer node in communications with the Internet for carrying out the method according to claim
 1. 15. A communications network in communications with the Internet having a computer node for carrying out the method according to claim
 1. 16. A computer-readable medium having instructions for carrying out the method according to claim
 1. 17. A system for incrementally maintaining a representation of the result of a computation uses k-th deltas of the specification, for k>1, comprising: a compilation algorithm configured to: create materialize representations of the computation results of multiple levels of the specification; create a first computer program to maintain the materialized representations concurrently; create a second computer program to maintain the materialized representation of level k under updates using the specifications of a level greater than k; and create a third computer program to perform the updates of the representation by the computation whose inputs are a state of the representation, update information related to the updates, and at least one of the representations of the specifications of a level greater than k; and a code generator configured to create machine code from the first computer program, the second computer program, and the third computer program.
 18. A query compiler for creating code to optimize queries of a database comprising: core data structures configured to compute a query representation; a parser configured to receive a query, said parser configured to receive said query representation, said parser configured to compute an initial query abstract syntax tree; a compilation algorithm configured to receive said initial query abstract syntax tree, said query representation, and delta queries, said compilation algorithm configured to eliminate variables and factor said delta queries; a query transformation library configured to receive said query representation, said query transformation library configured to simplify said delta queries, said query transformation library configured to provide the simplified delta queries to said compilation algorithm; and a query runtime configured to receive said query source code, said query runtime configured to provide map definitions and a computer program based on said query source code to a source code compiler to produce a compiled binary that includes code to optimize queries.
 19. A computer node in communications with the Internet for carrying out the method according to claim
 13. 20. A communications network in communications with the Internet having a computer node for carrying out the method according to claim
 13. 21. A computer-readable medium having instructions for carrying out the method according to claim
 13. 